package com.winton.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.winton.Z.Page;
import com.winton.dao.ItemDao;
import com.winton.model.S_ITEM;
import com.winton.service.ItemService;

@Service
public class ItemServiceImpl implements ItemService{
	
	@Autowired
	ItemDao itemDao;

	@Override
	public String addItem(S_ITEM item) {
		
		return itemDao.addItem(item);
	}

	@Override
	public List<S_ITEM> getItemListByUserid(int userid) {
		
		return itemDao.getItemListByUserid(userid);
	}

	@Override
	public Map<String, Object> queryItemList(S_ITEM item, int pageon) {
		
		Map<String,Object> map = new HashMap<String,Object>();
		Page page = new Page(pageon);
		String strWhere = " name like '%'||?||'%' ";
		Object[] objArr = { item.getName() };
	    page.setRowcountAndCompute(itemDao.selectItemListCount(strWhere,objArr));
	    map.put("page", page);
	    
	    List<S_ITEM> ilist =  itemDao.queryItemList(page,strWhere,objArr);
	    List<S_ITEM> list = new ArrayList<S_ITEM>();
	    for (S_ITEM it : ilist) {
	    	if(it.getPid()!=0) {
	    		it.setUpname( itemDao.findItemById(it.getPid()).getName() );
	    	}	    	
	    	list.add(it);
		}
	    
	    map.put("list",list);  
		
		return map;
	}

	@Override
	public List<S_ITEM> queryLevelItem() {
		
		return itemDao.queryLevelItem();
	}

	@Override
	public int deleteItemById(int id) {
		
		return itemDao.deleteItemById(id);
	}

	@Override
	public String updateItem(S_ITEM item) {
		
		return itemDao.updateItem(item);
	}

	@Override
	public List<S_ITEM> queryItemList() {
		
		return itemDao.queryItemList();
	}

	@Override
	public S_ITEM findItemById(int id) {
		// TODO Auto-generated method stub
		return itemDao.findItemById(id);
	}

	@Override
	public List<S_ITEM> findItemByPid(int id) {
		// TODO Auto-generated method stub
		return itemDao.findItemByPid(id);
	}

}
